from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, Float, ForeignKey, JSON
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import UUID
import uuid
from app.db.base import Base


class VideoTask(Base):
    """视频处理任务模型"""
    __tablename__ = "video_tasks"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    title = Column(String(255), nullable=False)
    description = Column(Text)

    # 文件信息
    original_filename = Column(String(255), nullable=False)
    original_file_url = Column(String(500))
    original_file_size = Column(Integer)  # 文件大小（字节）
    duration = Column(Float)  # 视频时长（秒）
    format = Column(String(20))  # 视频格式

    # 目标平台和模板
    target_platform = Column(String(50), nullable=False)  # 目标平台
    template_config = Column(JSON)  # 模板配置

    # 处理状态
    status = Column(String(20), default="pending")  # pending, processing, completed, failed
    progress = Column(Float, default=0.0)  # 进度百分比
    current_step = Column(String(100))  # 当前处理步骤

    # 错误信息
    error_message = Column(Text)
    error_details = Column(JSON)

    # 结果信息
    output_video_url = Column(String(500))
    thumbnail_url = Column(String(500))
    processing_time = Column(Float)  # 处理时间（秒）

    # 分析结果
    transcription_data = Column(JSON)  # 语音转录结果
    analysis_data = Column(JSON)  # 内容分析结果
    cut_segments = Column(JSON)  # 剪辑片段

    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    started_at = Column(DateTime(timezone=True))
    completed_at = Column(DateTime(timezone=True))

    # 关系
    user = relationship("User", back_populates="video_tasks")

    def __repr__(self):
        return f"<VideoTask(id='{self.id}', title='{self.title}', status='{self.status}')>"